<script type="text/javascript">
   /*function printInvoice(invoice) {
      
     //For all browsers except IE5 and IE6
       if (window.XMLHttpRequest){
          var xhr = new XMLHttpRequest);}

     //For IE5 and IE6
       else{
          var xhr = new ActiveXObject("Microsoft.XMLHTTP");}

           //If 
           xhr.onreadystatechange = function () {
              if (xhr.readyState == 4 && xhr.status == 200) {
                  var yay = xhr.result;
                  print(yay);
                  }
              }
         xhr.open("GET", "view.php?print=" + invoice);
         xhr.send(null);*/
        }


</script>


<?php

//This contains the output for the invoice sub menu (under clients)
//It allows the client to view a summary of all his invoices; and, if
//desired, view a more detailed .pdf version of the invoice. 

//Include php file that will allow the use of getInvoice function
include("GetInvoiceList.php");

   //Retrieve client info using the getClientInfo function
   $clientName = $_SESSION["clientname"];                     //Retrieve client name

     //Retrieve client invoice data using the getInvoice function
     $invoiceData = getInvoice($_SESSION['ClientID']);


     #For Convenience
     $size = count((array)$invoiceData);


   #Sort the invoices by date in reverse order. (Most recent first)
   #Arbitrary counters i and j
     for($i = 0; $i < $size; $i++){
     
           for($j = 0; $j < $size-$i; $j++){

                      #Splitting the dates for comparison
                      $one = explode("-", $invoiceData[$j]["date"]);
                        $two = explode("-", $invoiceData[$j+1]["date"]);

                      #***Making comparison and swap if the date is more recent***

                        #Year is bigger
                        if($one[0] < $two[0]){
                           #Swap the two elements
                           $temp = $invoiceData[$j];
                           $invoiceData[$j] = $invoiceData[$j+1];
                           $invoiceData[$j+1] = $temp;}

                        #Year is equal, month is bigger
                        if(($one[0] == $two[0]) && ($one[1] < $two[1])){
                             #Swap the two elements
                             $temp = $invoiceData[$j];
                             $invoiceData[$j] = $invoiceData[$j+1];
                             $invoiceData[$j+1] = $temp;} 

                        #Year is equal, month is equal, day is bigger
                        if(($one[0] == $two[0]) && ($one[1] == $two[1]) && ($one[2] < $two[2])){
                             #Swap the two elements
                             $temp = $invoiceData[$j];
                             $invoiceData[$j] = $invoiceData[$j+1];
                             $invoiceData[$j+1] = $temp;}       
                    }

           } 






     //****Main Content display of Invoices page****
     echo " <div id=\"BodyMain\">
                <h2 class = \"header1\">$clientName</h2>
                <br />";


   //Loop that will reiterate through all of the invoices, sort them, and display 
   //them to the screen. It also contains two buttons that will appear next to each
   //invoice summary, view and print.
   for ($x = 0; $x < $size; $x++){



   #format the invoice price using php regExp
     $invoiceData[$x]["bill"] = 
          preg_replace("/^-[0-9]*.?[0-9]*/", "(\$ ".substr($invoiceData[$x]["bill"], 1).")", $invoiceData[$x]["bill"]);
     $invoiceData[$x]["bill"] =
          preg_replace("/^\+[0-9]*.?[0-9]*/", "\$ ".substr($invoiceData[$x]["bill"], 1), $invoiceData[$x]["bill"]);
     $invoiceData[$x]["bill"] =
          preg_replace("/^[0-9]*.?[0-9]*$/", "\$ ".$invoiceData[$x]["bill"], $invoiceData[$x]["bill"]);

     $invoiceData[$x]["bill"] = strrev($invoiceData[$x]["bill"]);     #Reverse String
     $invoiceData[$x]["bill"] =
          preg_replace("/([0-9]{3})/", "$1,", $invoiceData[$x]["bill"]);
     $invoiceData[$x]["bill"] = strrev($invoiceData[$x]["bill"]);     #Re-reverse String
     $invoiceData[$x]["bill"] =
          preg_replace("/\s,([0-9,]+)/", " $1", $invoiceData[$x]["bill"]);
    
   #Setting up the path to the client's invoices
    $invoicePath = $invoiceData[$x]["filePath"];
    $pass = $invoicePath."/".$invoiceData[$x]["invoiceNumber"].".pdf";	

 #Print out each Invoice's information    
echo"<pre><p class=\"headerLeft\">Invoice#: <span class=largebodycopy>".$invoiceData[$x]["invoiceNumber"]."</span>     Issued:  <span class=largebodycopy>".$invoiceData[$x]["date"]."</span>     Owed:  <span class=largebodycopy>".$invoiceData[$x]["bill"]."</span></p></pre>

          <form method=\"get\" action=\"../phplib/view.php\">
                <!--View PDF-->
    		<button class=\"FloatLeft\" type=\"submit\" name=\"view\" value=\"$pass\" />View</button>  
    	  </form>
                <!--Print PDF-->           
                <button value=\"$pass\" name=\"print\"  onclick=\"printInvoice('\".$pass.\"')\">Print</button>";

    }

//Underbar quick fix
echo "<div id=\"BodyMain\"> <br /><br /><br /><br /><br /><br/><br/><br/><br/><br/><br/><br/><br/></div>"


?>
